<?php

namespace app\models;

use Yii;
//use app\models\Qa;
use app\models\Tags;
use app\models\Qa_tag;

/**
 * This is the model class for table "qa_tag".
 *
 * @property string $qid
 * @property string $tid
 */
class Qa_tag extends \yii\db\ActiveRecord {

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'qa_tag';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['qid', 'tid'], 'required'],
            [['qid', 'tid'], 'integer']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'qid' => '问答ID',
            'tid' => '标签ID',
        ];
    }

    public function getQa() {
        return $this->hasOne(Qa::className(), ['id' => 'qid']);
    }

    public function getTag() {
        return $this->hasOne(Tags::className(), ['id' => 'tid']);
    }

    /**
     * 获取用于展示的tags
     * @param int $id
     * @return array
     */
    public function get_show_tags($id) {
        $new_tag_list = array();
        $qa_tag_list = Qa_tag::find()->where(['qid' => $id])->asArray()->all();
        foreach ($qa_tag_list as $row) {
            $tag_info = Tags::findOne(['id' => $row['tid']]);
            if ($tag_info) {
                $new_tag_list[] = $tag_info;
            }
        }
        return $new_tag_list;
    }

    /**
     * 获取编辑使用的ID
     * @param int $id
     * @return string
     */
    public function get_edit_tags($id) {
        $new_list = array();
        $qa_tag_list = Qa_tag::find()->where(['qid' => $id])->asArray()->all();
        foreach ($qa_tag_list as $row) {
            $tag_info = Tags::findOne(['id' => $row['tid']]);
            if ($tag_info) {
                $new_list[] = $tag_info->name;
            }
        }
        $tags = implode(',', $new_list);
        return $tags;
    }

    /**
     * 保存tags
     * @param object $question
     * @param string $tags
     */
    public function save_tags($question, $tags = NULL) {
        if ($tags) {
            $tags_arr = explode(',', $tags);
            foreach ($tags_arr as $row) {
                $result = $this->check_tag_by_name($row);
                if ($result) {
                    $qa_tag = new Qa_tag();
                    $qa_tag->qid = $question->id;
                    $qa_tag->tid = $result;
                    $qa_tag->save();
                } else {
                    //创建tag
                    $tags = new Tags();
                    $tags->name = $row;
                    $tags->save();
                    //创建qa_tag
                    $qa_tag = new Qa_tag();
                    $qa_tag->qid = $question->id;
                    $qa_tag->tid = $tags->id;
                    $qa_tag->save();
                }
            }
        }
    }

    /**
     * 
     * @param object $question
     * @param string $tags
     */
    public function update_tag($question, $tags = NULL) {
        if ($tags) {
            $tags_arr = explode(',', $tags);
            foreach ($tags_arr as $row) {
                //1、检查tag是否已经存在
                $tag_result = $this->check_tag_by_name($row);
                if ($tag_result) {//数据库存在tags的情况
                    //2、检查qa_tag是否已经存在
                    $qa_tag_result = Qa_tag::findOne(['qid' => $question->id, 'tid' => $tag_result]);
                    if (!$qa_tag_result) {
                        $qa_tag = new Qa_tag();
                        $qa_tag->qid = $question->id;
                        $qa_tag->tid = $tag_result;
                        $qa_tag->save();
                    }
                } else {//数据库不存在tags的情况
                    //创建tag
                    $tags = new Tags();
                    $tags->name = $row;
                    $tags->save();
                    //创建qa_tag
                    $qa_tag = new Qa_tag();
                    $qa_tag->qid = $question->id;
                    $qa_tag->tid = $tags->id;
                    $qa_tag->save();
                }
            }
        }
    }

    protected function check_tag_by_name($name) {
        $result = Tags::findOne(['name' => $name]);
        if ($result) {
            return $result['id'];
        } else {
            return false;
        }
    }

}
